using System;
using System.Drawing;
using System.Windows.Forms;
using Extensions;
using ConfOrder.Properties;
using ConfOrder;

namespace ConfExpert.Entities
{
    public partial class ProductViewer : XPopupForm
    {
        int ProductId;
        public ProductViewer(int productId)
        {
            InitializeComponent();
            ProductId = productId;
            this.MouseWheel += new MouseEventHandler(DetailView_MouseWheel);
        }
         
        BindingSource bs;

        Bitmap GetKumas()
        {
            if (ds.xProductPhotos.FindByProductId(ProductId) == null)
                return Resources.NotReadyBig;
            else
                if (ds.xProductPhotos.FindByProductId(ProductId).IsPhotoNull())
                    return Resources.NotReadyBig;
                else
                    return ds.xProductPhotos.FindByProductId(ProductId).Photo.ToBitmap();
        }

        
        private void DetailView_Load(object sender, EventArgs e)
        {
            GetDetails("xProductPhotos;xBuffer", "GetProductPhoto", ProductId, false);
            bs = new BindingSource(ds, "xProductPhotos");

            ruler.ProductId = ProductId;
            ruler.Kumas = GetKumas();
            memoEdit1.DataBindings.Add("EditValue", ds, "xBuffer.strVal");

            //theImage = Image.FromStream(new MemoryStream(((byte[])((DataRowView)bsEkle.Current).Row["BLOB"])));

            this.SizeChanged += new EventHandler(DetailView_SizeChanged);
            this.Shown += new EventHandler(DetailView_Shown);
            ratio = Convert.ToDecimal(this.Height) / Convert.ToDecimal(this.Width);
        }

        decimal ratio = 1;

        void DetailView_Shown(object sender, EventArgs e)
        {
            memoEdit1.Select(memoEdit1.Text.Length, 0);
        }

        FormWindowState prevState = FormWindowState.Normal;
        void DetailView_SizeChanged(object sender, EventArgs e)
        {
            this.SuspendLayout();
            bool stateChanged = this.WindowState != prevState;

            if (stateChanged)
            {
                ds.xProductPhotos.Clear();
                GetDetails("xProductPhotos;xBuffer", "GetProductPhoto", ProductId, this.WindowState == FormWindowState.Maximized);                
                prevState = this.WindowState;
            }

            ruler.Kumas = GetKumas();
            
            if (stateChanged && prevState == FormWindowState.Normal)            
                ruler.ReDraw();
            
            this.ResumeLayout();
        }


        void DetailView_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta != 0 && (this.Height > 100 || e.Delta < 0))
            {
                this.SuspendLayout();
                this.Width -= e.Delta / 3;
                this.Height -= e.Delta / 3;
                this.ResumeLayout();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ruler.ReDraw();
        }


    }
}